package com.ftg.learn;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * javabase servlet 配置文件 ，用于替换web.xml
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/30 上午8:43
 */
public class JavaServletConfig implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {
        //为spring 底层的servlet ServletContext 提供相应的参数
        //spring
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.scan("com.ftg.learn");
//        applicationContext.refresh();
        //DispatcherServlet

        //servlet 注册功能
        ServletRegistration.Dynamic web = servletContext.addServlet("springmvcservlet",new DispatcherServlet(applicationContext));
        web.setLoadOnStartup(1);
        web.addMapping("/api/*");
    }

}
